package com.qc.wintrax.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Build;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/8/29.
 */

public class ElevationView extends View {

    //    private int xPoint = 0;// 原点X坐标
//    private int yPoint = 0;// 原点Y坐标
//    private int xLengh = 240;// X轴长度
//    private int yLengh = 320;// Y轴长度
//    private int xScale = 5;// X轴一个刻度长度
//    private int yScale = 5;// Y轴一个刻度长度
//    private int widthBorder = 40;// 内边缘宽度，为了统计图不靠在屏幕的边缘上，向边缘缩进距离。最好大于30。
//    private String[] xLableArray;// X轴标签
//    private int[] yLableArray;// Y轴标签
//    private int[] dataArray;// 画折线的数值
    private Paint mPaint;
    private Rect mRect;
    private int mWidth;
    private int mHeight;
    private int mPaddingStart;
    private int mPaddingEnd;
    private int mPaddingTop;
    private int mPaddingBottom;
    private int mLeft;
    private int mTop;
    private int mRight;
    private int mBottom;
    private Context mContext;
    private List<Bar> mBarLists;

    public ElevationView(Context context) {
        super(context);
        mContext = context;
        initData();
    }

    public ElevationView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initData();
    }

    private void initData() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mRect = new Rect();
        // default data
//        mBarLists = new ArrayList<Bar>();
//        Bar bar1 = new Bar(1, 0.3f, Color.CYAN, "one", "30");
//        Bar bar2 = new Bar(2, 0.55f, Color.GREEN, "two", "55");
//        Bar bar3 = new Bar(3, 0.8f, Color.BLUE, "three", "80");
//        mBarLists.add(bar1);
//        mBarLists.add(bar2);
//        mBarLists.add(bar3);
    }

    public void setBarLists(ArrayList<Bar> barLists) {
        mBarLists = barLists;
        postInvalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        mWidth = getSizeFromMeasureSpec(widthMeasureSpec, 540);
        mHeight = getSizeFromMeasureSpec(heightMeasureSpec, 480);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            mPaddingStart = getPaddingStart();
            mPaddingEnd = getPaddingEnd();
        }
        mPaddingTop = getPaddingTop();
        mPaddingBottom = getPaddingBottom();
        mLeft = mPaddingStart;
        mTop = mPaddingTop;
        mRight = mWidth - mPaddingEnd;
        mBottom = mHeight - mPaddingBottom;
        setMeasuredDimension(mWidth, mHeight);
    }

    /**
     * 实例化值
     *
     * @param screenWidth  手机屏幕宽度
     * @param ScreenHeight 手机屏幕高度
     * @param xLable       X轴标签
     * @param yLable       Y轴标签
     * @param dataArray    统计数据
     */
    public void initValue(int screenWidth, int ScreenHeight, String[] xLable, int[] yLable, int[] dataArray) {
        // 38 是手机状态栏的大概高度，这是i9100上测试的，如果应用有标题栏需要在这个基础上加，差不多两倍吧。
//        xPoint += widthBorder;
//        yPoint = ScreenHeight - widthBorder - 60;
//        xLengh = screenWidth - widthBorder * 2;
//        yLengh = ScreenHeight - widthBorder * 2 - 38;
////        xScale = getScale(xLable.length - 1, xLengh);
////        yScale = getScale(yLable.length - 1, yLengh);
//        xLableArray = xLable;
//        yLableArray = yLable;
//        this.dataArray = dataArray;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // set background
        canvas.drawColor(Color.RED);
        mRect.set(mLeft, mTop, mRight, mBottom);
        mPaint.setColor(Color.YELLOW);
        canvas.drawRect(mRect, mPaint);
        //*/

//        // 设置底部文字属性
//        mPaint.setTextSize(sp2Px(mContext, 11));
        mPaint.setTextAlign(Paint.Align.CENTER);
        Paint.FontMetricsInt fontMetricsInt = mPaint.getFontMetricsInt();
        int fontHeight = (int) Math.ceil(fontMetricsInt.bottom - fontMetricsInt.top);

        int N = mBarLists.size();
        int UNIT_WIDTH = (mRight - mLeft) / (2 * N + 1);
        int left = 0;
        int top = 0;
        int right = 0;
        int bottom = 0;

        // 逐个画bar
        for (int i = 0; i < N; i++) {
            Bar bar = mBarLists.get(i);
            // 画 bar 底部文字
            left = (int) (mLeft + i);
            right = left;
            top = mBottom - fontHeight;
            bottom = mBottom;
            mRect.set(left, top, 0, 0);
            int baseLine = (mRect.top + mRect.bottom - fontMetricsInt.top - fontMetricsInt.bottom) / 2;
            mPaint.setColor(Color.BLACK);
            canvas.drawText(bar.bootomText, mRect.centerX(), baseLine, mPaint);
            // 画 bar 图形
            left = i+1;
            right = left +UNIT_WIDTH;
            bottom = mBottom - fontHeight;
            top = bottom - (int) ((mBottom - mTop - fontHeight * 2) * bar.ratio);
            mRect.set(left, top, right, bottom);
            mPaint.setColor(bar.color);
            canvas.drawRect(mRect, mPaint);
            // 画 bar 顶部文字
            left = (int) (mLeft + (i * 2 + 0.5f) * UNIT_WIDTH);
            right = left + UNIT_WIDTH * 2;
            bottom = top;
            top = top - fontHeight;
            mRect.set(left, top, right, bottom);
            baseLine = (mRect.top + mRect.bottom - fontMetricsInt.top - fontMetricsInt.bottom) / 2;
            mPaint.setColor(Color.BLACK);
            canvas.drawText(bar.topText, mRect.centerX(), baseLine, mPaint);
        }
        // 画线
        mPaint.setColor(Color.BLACK);
        canvas.drawLine(mLeft, mBottom - fontHeight, mRight, mBottom - fontHeight, mPaint);
        // canvas.drawLine(mLeft, mTop + fontHeight, mRight, mTop + fontHeight, mPaint);
        super.onDraw(canvas);
        // 设置画笔
//        Paint paint = new Paint();
//        paint.setStyle(Paint.Style.STROKE);// 设置画笔样式
//        paint.setAntiAlias(true);// 去锯齿
//        paint.setColor(Color.BLACK);// 设置颜色
//        paint.setTextSize(12);// 设置字体
//        // 画X轴轴线
//        canvas.drawLine(xPoint, yPoint, xPoint + xLengh, yPoint, paint);
//        // 画X轴箭头
//        canvas.drawLine(xPoint + xLengh - 6, yPoint - 3, xPoint + xLengh,
//                yPoint, paint);
//        canvas.drawLine(xPoint + xLengh - 6, yPoint + 3, xPoint + xLengh,
//                yPoint, paint);
//        // 画Y轴轴线
//        canvas.drawLine(xPoint, yPoint, xPoint, yPoint - yLengh, paint);
//        // 画Y轴箭头
//        canvas.drawLine(xPoint + 3, yPoint - yLengh + 6, xPoint, yPoint
//                - yLengh, paint);
//        canvas.drawLine(xPoint - 3, yPoint - yLengh + 6, xPoint, yPoint
//                - yLengh, paint);
//
//        for (int i = 0; xLableArray != null && i < xLableArray.length; i++) {
//            // 画X轴刻度
//            canvas.drawLine(xPoint + xScale * i, yPoint - 3, xPoint + xScale
//                    * i, yPoint, paint);
//            // 画X轴刻度标签
//            canvas.drawText(xLableArray[i], xPoint + xScale * i - 16,
//                    yPoint + 15, paint);
//
//
//            // 画折线
////            if (dataArray != null && dataArray.length > 0 && i < dataArray.length) {
////                int ydata = getYDataPoint(dataArray[i]);
////                if (ydata != 0) {
////                    canvas.drawCircle(xPoint + xScale * i,ydata, 2, paint);
////                    if(dataArray.length>i+1){
////                        canvas.drawLine(xPoint + xScale * i,getYDataPoint(dataArray[i]), xPoint + xScale * (i+1), getYDataPoint(dataArray[i+1]), paint);
////                    }
////                }
////            }
//        }
//
//        for (int i = 0; yLableArray != null && i < yLableArray.length; i++) {
//            // 画Y轴刻度
//            canvas.drawLine(xPoint, yPoint - yScale * i, xPoint + 3, yPoint
//                    - yScale * i, paint);
//            // 画Y轴刻度标签
//            canvas.drawText(String.valueOf(yLableArray[i]), xPoint - 30, yPoint
//                    - yScale * i + 3, paint);
//        }
    }

    /**
     * 得到每一等分的长度
     *
     * @param num    所要分成的等份
     * @param length 要分割的总长度
     * @return
     */
    private int getScale(int num, int length) {
        if (num > 0 && length > 0) {
            length -= 10;// 为了美观，缩进
            length = length - (length % num);
            return length / num;
        } else {
            return 0;
        }
    }

    // 工具类
    public static int getSizeFromMeasureSpec(int measureSpec, int defaultSize) {
        int result = 0;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);
        if (mode == MeasureSpec.EXACTLY) {
            result = size;
        } else {
            result = defaultSize;
            if (mode == MeasureSpec.AT_MOST) {
                result = Math.min(defaultSize, size);
            }
        }
        return result;
    }

    public static float sp2Px(Context context, float sp) {
        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        display.getMetrics(metrics);
        float px = metrics.scaledDensity;
        return sp * px;
    }

    public class Bar {
        public int id;
        public float ratio;
        public int color;
        public String bootomText;
        public String topText;

        public Bar(int id, float ratio, int color, String bootomText, String topText) {
            this.id = id;
            this.ratio = ratio;
            this.color = color;
            this.bootomText = bootomText;
            this.topText = topText;
        }
    }
}
